package com.nuoniu.sibanyun.controller.production;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.ProductionWorkSearchDto;
import com.nuoniu.sibanyun.entity.dto.VProductionWorkMainSearchDto;
import com.nuoniu.sibanyun.entity.dto.VProductionWorkMainSplitDto;
import com.nuoniu.sibanyun.entity.dto.statistics.ProductionWorkCumSummaryDto;
import com.nuoniu.sibanyun.entity.dto.statistics.ProductionWorkInAndPickDto;
import com.nuoniu.sibanyun.entity.dto.statistics.ProductionWorkMonthSummaryDto;
import com.nuoniu.sibanyun.entity.production.VProductionWorkDetail;
import com.nuoniu.sibanyun.entity.production.VProductionWorkMain;
import com.nuoniu.sibanyun.entity.vo.UnFinishProductionShowVo;
import com.nuoniu.sibanyun.service.production.IVProductionWorkDetailService;
import com.nuoniu.sibanyun.service.production.IVProductionWorkInventoryDetailService;
import com.nuoniu.sibanyun.service.production.IVProductionWorkMainService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 生产加工单 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-03-13
 */
@RestController
@RequestMapping("/production/work")
@Api(value = "生产加工", tags = "生产加工")
public class VProductionWorkMainController extends BaseController {

    @Autowired
    private IVProductionWorkMainService productionWorkMainService;
    @Autowired
    private IVProductionWorkDetailService productionWorkDetailService;
    @Autowired
    private IVProductionWorkInventoryDetailService productionWorkInventoryDetailService;

    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(productionWorkMainService.getCode());
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VProductionWorkMain productionWorkMain) {
        productionWorkMainService.saveProductionWorkMain(productionWorkMain);
        return HttpResult.ok();
    }

    @ApiOperation("保存（分单处理）")
    @PostMapping("saveOrderForInventory")
    public HttpResult saveOrderForInventory(@RequestBody VProductionWorkMainSplitDto productionWorkMainSplitDto) {
        productionWorkMainService.saveOrderForInventory(productionWorkMainSplitDto);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        productionWorkMainService.submit(id, status);
        return HttpResult.ok();
    }
    /**
     * 审核/弃审
     * @author 小啊刚
     * @date 2020-11-25
     */
    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        if (null == status || status < 0 || status > 1)
            throw new NuoNiuException("状态参数错误");
        productionWorkMainService.examine(id,status);
        return HttpResult.ok();
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        productionWorkMainService.deleteById(id);
        return HttpResult.ok();
    }




    @ApiOperation("根据车间ID等条件 查询 未完工 单据")
    @GetMapping("listNotFinishMain")
    public HttpResult<List<VProductionWorkMain>> listNotFinishMain(VProductionWorkMainSearchDto productionWorkMainSearchDto) {
        return HttpResult.ok(productionWorkMainService.listNotFinishMain(productionWorkMainSearchDto));
    }


    @ApiOperation("根据车间ID等条件 查询 未完工 单据_2")
    @GetMapping("listNotFinishMain2")
    public HttpResult<List<VProductionWorkMain>> listNotFinishMain2(VProductionWorkMainSearchDto productionWorkMainSearchDto) {
        return HttpResult.ok(productionWorkMainService.listNotFinishMain2(productionWorkMainSearchDto));
    }


    @ApiOperation("根据加工单ID 查询 未完工 产品明细")
    @GetMapping("listNotDispatchedDetail")
    public HttpResult<List<VProductionWorkDetail>> listNotDispatchedDetail(Integer mainId) {
        return HttpResult.ok(productionWorkDetailService.listNotDispatchedDetail(mainId));
    }

    @ApiOperation("根据加工单ID 查询 未完工 产品明细_2")
    @GetMapping("listNotDispatchedDetail2")
    public HttpResult<List<VProductionWorkDetail>> listNotDispatchedDetail2(Integer mainId) {
        return HttpResult.ok(productionWorkDetailService.listNotDispatchedDetail2(mainId));
    }

    /************** 委外加工单  *************/


    @ApiOperation("获取 委外加工单 编码")
    @GetMapping("getDelegateCode")
    public HttpResult getDelegateCode() {
        return HttpResult.ok(productionWorkMainService.getDelegateCode());
    }

    @ApiOperation("委外加工单 保存")
    @PostMapping("saveDelegateOrder")
    public HttpResult saveDelegateOrder(@RequestBody VProductionWorkMain productionWorkMain) {
        productionWorkMainService.saveProductionWorkMain(productionWorkMain);
        return HttpResult.ok();
    }


    /**
     * 委外加工单 审核/弃审
     * @date 2020-11-25
     */
    @GetMapping("examineDelegate")
    @ApiOperation("委外加工单 审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examineDelegate(Long id, Integer status) {
        if (null == status || status < 0 || status > 1)
            throw new NuoNiuException("状态参数错误");
        productionWorkMainService.examineDelegate(id,status);
        return HttpResult.ok();
    }

    @ApiOperation("委外加工单 删除")
    @DeleteMapping("deleteDelegateById")
    public HttpResult deleteDelegateById(Long id) {
        productionWorkMainService.deleteDelegateById(id);
        return HttpResult.ok();
    }


    @ApiOperation("获取 加工单 材料明细")
    @GetMapping("listByProductionWorkId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productionWorkMainId", value = "加工单 id"),
    })
    public HttpResult listByProductionWorkId(Long productionWorkMainId) {
        return HttpResult.ok(productionWorkInventoryDetailService.listByProductionWorkId(productionWorkMainId));
    }


    @ApiOperation("获取 加工单 材料明细 和 工序明细")
    @GetMapping("getVProductionWorkDetailSimpleVo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productionWorkMainId", value = "加工单 id"),
    })
    public HttpResult getVProductionWorkDetailSimpleVo(Long productionWorkMainId) {
        return HttpResult.ok(productionWorkDetailService.getVProductionWorkDetailSimpleVo(productionWorkMainId));
    }




    @ApiOperation("查询 未完工 的加工单 明细 和 材料明细")
    @GetMapping("listUnFinishProductionShowVo")
    public HttpResult<UnFinishProductionShowVo> listUnFinishProductionShowVo(ProductionWorkSearchDto delegateProductionWorkSearchDto) {
        return HttpResult.ok(productionWorkMainService.listUnFinishProductionShowVo(delegateProductionWorkSearchDto));
    }



    /*********************   生产制造 报表统计 开始  **********************/

    @ApiOperation("统计：生产订单执行汇总表")
    @PostMapping("pageProductionWorkCumSummaryVo")
    public HttpResult pageProductionWorkCumSummaryVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionWorkCumSummaryVo(dto));
    }

    @ApiOperation("统计：生产订单执行明细表")
    @PostMapping("pageProductionWorkCumDetailVo")
    public HttpResult pageProductionWorkCumDetailVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionWorkCumDetailVo(dto));
    }

    @ApiOperation("统计：生产订单领料汇总表")
    @PostMapping("pageProductionPickReturnCumSummaryVo")
    public HttpResult pageProductionPickReturnCumSummaryVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionPickReturnCumSummaryVo(dto));
    }

    @ApiOperation("统计：生产订单领料明细表")
    @PostMapping("pageProductionPickReturnCumDetailVo")
    public HttpResult pageProductionPickReturnCumDetailVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionPickReturnCumDetailVo(dto));
    }

    @ApiOperation("统计：生产领料异常表")
    @PostMapping("pageProductionPickAbnormalVo")
    public HttpResult pageProductionPickAbnormalVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionPickAbnormalVo(dto));
    }

    @ApiOperation("统计：生产领料差异汇总表")
    @PostMapping("pageProductionPickAbnormalSumVo")
    public HttpResult pageProductionPickAbnormalSumVo(@RequestBody ProductionWorkCumSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionPickAbnormalSumVo(dto));
    }


    @ApiOperation("统计：生产入库月报表")
    @PostMapping("pageProductionMonthInVo")
    public HttpResult pageProductionMonthInVo(@RequestBody ProductionWorkMonthSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionMonthInVo(dto));
    }


    @ApiOperation("统计：生产领料月报表")
    @PostMapping("pageProductionPickReturnMonthSummaryVo")
    public HttpResult pageProductionPickReturnMonthSummaryVo(@RequestBody ProductionWorkMonthSummaryDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionPickReturnMonthSummaryVo(dto));
    }

    @ApiOperation("统计：生产入库领料查询 -- 产品入库情况")
    @PostMapping("pageProductionInMainVo")
    public HttpResult pageProductionInMainVo(@RequestBody ProductionWorkInAndPickDto dto) {
        return HttpResult.ok( productionWorkMainService.pageProductionInMainVo(dto));
    }


    @ApiOperation("统计：生产入库领料查询 -- 对应产品领料情况")
    @PostMapping("listProductionPickVo")
    public HttpResult listProductionPickVo(@RequestBody ProductionWorkInAndPickDto dto) {
        return HttpResult.ok( productionWorkMainService.listProductionPickVo(dto));
    }



    /*********************   生产制造 报表统计 结束  **********************/




}
